home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / gfxfx / polygons.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-14  |  3KB  |  109 lines

  1.  
  2. {$r+}
  3. program polygons;
  4. uses crt;
  5. const vidseg:word=$a000;
  6.  
  7. procedure horline(xb,xe,y:integer; c:byte); assembler;
  8. asm
  9.   mov bx,[xb]
  10.   cmp bx,0              { if zero don't draw }
  11.   jz @out
  12.   cmp bx,320
  13.   ja @out
  14.   mov cx,[xe]
  15.   jcxz @out
  16.   cmp cx,320
  17.   ja @out
  18.   cmp bx,cx             { see if x-end is smaller than x-begin }
  19.   jb @skip
  20.   xchg bx,cx            { yes: switch coords }
  21.  @skip:
  22.   inc cx
  23.   sub cx,bx             { length of line in cx }
  24.   mov es,vidseg         { segment to draw in }
  25.   mov ax,[y]            { heigth of line }
  26.   shl ax,6
  27.   mov di,ax
  28.   shl ax,2
  29.   add di,ax             { y*320 in di (offset) }
  30.   add di,bx             { add x-begin }
  31.   mov al,[c]            { get color }
  32.   shr cx,1              { div length by 2 }
  33.   jnc @skip2            { carry set? }
  34.   stosb                 { draw byte }
  35.  @skip2:
  36.   mov ah,al             { copy color in hi-byte }
  37.   rep stosw             { draw (rest of) line }
  38.  @out:
  39. end;
  40.  
  41. procedure polygon(x1,y1,x2,y2,x3,y3,x4,y4:integer; c:byte);
  42. var
  43.   xpos:array[0..199,0..1] of integer;
  44.   mny,mxy,y:integer;
  45.   i:word;
  46.   s1,s2,s3,s4:shortint;
  47.   pos:byte;
  48. begin
  49.   mny:=y1;
  50.   if y2<mny then mny:=y2;
  51.   if y3<mny then mny:=y3;
  52.   if y4<mny then mny:=y4;
  53.  
  54.   mxy:=y1;
  55.   if y2>mxy then mxy:=y2;
  56.   if y3>mxy then mxy:=y3;
  57.   if y4>mxy then mxy:=y4;
  58.  
  59.   s1:=byte(y1<y2)*2-1;
  60.   s2:=byte(y2<y3)*2-1;
  61.   s3:=byte(y3<y4)*2-1;
  62.   s4:=byte(y4<y1)*2-1;
  63.  
  64.   y:=y1; pos:=byte(y1<y2);
  65.   if y1<>y2 then repeat
  66.     xpos[y,pos]:=integer(x2-x1)*(y-y1) div (y2-y1)+x1;
  67.     inc(y,s1);
  68.   until y=y2+s1 else xpos[y,pos]:=x1;
  69.   y:=y2; pos:=byte(y2<y3);
  70.   if y2<>y3 then repeat
  71.     xpos[y,pos]:=integer(x3-x2)*(y-y2) div (y3-y2)+x2;
  72.     inc(y,s2);
  73.   until y=y3+s2 else xpos[y,pos]:=x2;
  74.   y:=y3; pos:=byte(y3<y4);
  75.   if y3<>y4 then repeat
  76.     xpos[y,pos]:=integer(x4-x3)*(y-y3) div (y4-y3)+x3;
  77.     inc(y,s3);
  78.   until y=y4+s3 else xpos[y,pos]:=x3;
  79.   y:=y4; pos:=byte(y4<y1);
  80.   if y4<>y1 then repeat
  81.     xpos[y,pos]:=integer(x1-x4)*(y-y4) div (y1-y4)+x4;
  82.     inc(y,s4);
  83.   until y=y1+s4 else xpos[y,pos]:=x4;
  84.   for y:=mny to mxy do
  85.     horline(xpos[y,0],xpos[y,1],y,c);
  86. end;
  87.  
  88. var
  89.   time:longint absolute $0:$46c;
  90.   ctr,time1,endtime1:longint;
  91.   x1,x2,i:word; y,c:byte;
  92. begin
  93.   asm mov ax,13h; int 10h; end;
  94.   randomize;
  95.   ctr:=0;
  96.   time1:=time;
  97.   repeat
  98.     polygon(random(300)+10,random(180)+10,random(300)+10,random(180)+10,random(300)+10,random(180)+10,
  99.             random(300)+10,random(180)+10,random(256));
  100.     inc(ctr);
  101.   until keypressed;
  102.   while keypressed do readkey;
  103.   endtime1:=time;
  104.   textmode(lastmode);
  105.   write(ctr,' polygons in ',((endtime1-time1)/18.2):0:2,' sec. -> ');
  106.   writeln('Polygons per second: ',(ctr/((endtime1-time1)/18.2)):0:2,' (±185?)');
  107.   while not keypressed do; while keypressed do readkey;
  108. end.
  109.